package com.java1234.jxc.beans;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.DB;
import org.nutz.dao.entity.annotation.*;

import java.io.Serializable;
import java.util.Date;

@Table("t_log")
public class SysLog implements Serializable{

    public final static String LOGIN_ACTION="登录操作";
    public final static String LOGOUT_ACTION="注销操作";
    public final static String SEARCH_ACTION="查询操作";
    public final static String UPDATE_ACTION="更新操作";
    public final static String ADD_ACTION="添加操作";
    public final static String DELETE_ACTION="删除操作";

    @Column
    @Comment("编号")
    @Id
    @Prev({
            @SQL(db= DB.MYSQL,value = "SELECT IFNULL(MAX(id),0)+1 FROM t_log"),
            @SQL(db= DB.ORACLE,value = "SELECT COALESCE(MAX(id),0)+1 FROM t_log")
    })
    private Integer id;


    @Column
    @Comment("日志类型")
    @ColDefine(type = ColType.VARCHAR, width = 100)
    private String type;


    @Prev(els = @EL("$me.uid()"))
    @Column("user_id")
    @Comment
    private Integer userId;


    @One(field = "userId")
    private SysUser user;

    @Column
    @Comment("操作内容")
    @ColDefine(type = ColType.TEXT)
    private String content;


    @Column
    @Comment("操作日期")
    @Prev(els = @EL("$me.opatDate()"))
    @ColDefine(type = ColType.DATETIME)
    private Date time;

    public SysLog(){}

    public SysLog(String type,String content){
        this.type = type;
        this.content=content;

    }

    public Integer uid() {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (subject != null) {
                SysUser currentUser = (SysUser) subject.getSession().getAttribute("currentUser");

                return currentUser.getId();
            }
        } catch (Exception e) {
        }
        return null;
    }
    public Date opatDate(){
        return new Date();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public SysUser getUser() {
        return user;
    }

    public void setUser(SysUser user) {
        this.user = user;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }
}
